package io.vov.vitamio.adapter;

import io.vov.vitamio.entity.TVProgram;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ndjh.onlinetv.R;

public class TVProgramsListViewDescAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater mInfInflater;
	private List<TVProgram> mProgramsListDesc = new ArrayList<TVProgram>();
	private TVProgram mCurrentProgram;
	private TVProgram mProgram;
	public TVProgramsListViewDescAdapter(Context contect, List programsList, int position, TVProgram CurrentProgram) {
		mContext = contect;
		mInfInflater = LayoutInflater.from(contect);
		mCurrentProgram = CurrentProgram;
		
		int i = position * 7;
		int iEnd = i + 7;
		while ((i < programsList.size()) && (i < iEnd)) {
				mProgramsListDesc.add((TVProgram) programsList.get(i));
			i++;
		}
	}

	@Override
	public int getCount() {
		
		return mProgramsListDesc!=null? 7:0;
	}

	@Override
	public Object getItem(int position) {

		return mProgramsListDesc != null ? mProgramsListDesc.get(position):null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ProgramsListDescHolder holder;
		if (convertView == null) {
			convertView = mInfInflater.inflate(R.layout.tv_programs_item_desc, null);
			holder = new ProgramsListDescHolder(convertView);
			convertView.setTag(holder);
		}else {
			holder = (ProgramsListDescHolder) convertView.getTag();
		}
		if(mProgramsListDesc.size() > position){
		mProgram = mProgramsListDesc.get(position);
		
		String dateStr = mProgram.getPlayTime();
		dateStr = dateStr.split(" ")[1];
		dateStr = dateStr.substring(0,dateStr.lastIndexOf(":"));
		
		holder.mTVProgramsTime.setText(dateStr);
		holder.mTVProgramsDesc.setText(mProgram.getTitle());
		if(mProgram == mCurrentProgram){
			convertView.setBackgroundResource(R.drawable.tv_programs_item_desc_cell_highlight);
		}
//		else{
//			convertView.setBackgroundResource(R.drawable.tv_programs_item_desc_cell_default);
//		}
		}
		return convertView;
	}
	
	 private class ProgramsListDescHolder{
			private TextView mTVProgramsTime;
			private TextView mTVProgramsDesc;
			public ProgramsListDescHolder(View view){
				this.mTVProgramsTime = (TextView) view.findViewById(R.id.tv_programs_time);
				this.mTVProgramsDesc = (TextView) view.findViewById(R.id.tv_programs_desc);
			}
		}

}
